package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import tech.aistar.model.entity.Product;
import tech.aistar.model.entity.ProductType;
import tech.aistar.model.vo.ProductQueryVo;
import tech.aistar.service.IProductService;
import tech.aistar.service.IProductTypeService;
import tech.aistar.service.impl.ProductServiceImpl;
import tech.aistar.service.impl.ProductTypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 本类用来演示: 产品的控制层
 *
 * @author: success
 * @date: 2021/4/21 2:09 下午
 */
@WebServlet(urlPatterns = "/product/*")
public class ProductController extends HttpServlet {
    private IProductService productService = new ProductServiceImpl();

    private IProductTypeService productTypeService = new ProductTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getPathInfo();


        switch (path){
            case "/index":
                String login = req.getParameter("login");
                if(null!=login){
                    req.setAttribute("login",login);
                }

                //获取pageNow
                String pageNow = req.getParameter("pageNow");
                if(null == pageNow || pageNow.trim().length()==0){
                    pageNow = "1";
                }

                //判断更多条件是否显示
                String count = req.getParameter("count");
                if(count!=null){
                    req.setAttribute("count",count);
                }

                String typeId = req.getParameter("typeId");

                String minPrice = req.getParameter("minPrice");
                if(minPrice!=null){
                    req.setAttribute("minPrice",minPrice);
                }

                String searchName = req.getParameter("searchName");
                ProductQueryVo queryVo = new ProductQueryVo();
                queryVo.setName(searchName);
                if(typeId!=null){
                    queryVo.setProductTypeId(Integer.valueOf(typeId));
                    req.setAttribute("typeId",typeId);
                }
                if(minPrice!=null && minPrice.trim().length()>0){
                    queryVo.setMinPrice(Double.valueOf(minPrice));
                }

                List<ProductType> productTypeList = productTypeService.findAll();
                req.setAttribute("typeList",productTypeList);

                if(searchName!=null && searchName.trim().length()>0){
                    req.setAttribute("searchName",searchName);
                }

                PageInfo<Product> pageInfo = productService.findAll(queryVo,Integer.valueOf(pageNow),4);

                //放入到request作用域中
                req.setAttribute("pageInfo",pageInfo);

                //转发到首页页面...
                req.getRequestDispatcher("/WEB-INF/product/index.jsp").forward(req,resp);
                break;
        }
    }
}
